package com.eastfair.venueservice.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.eastfair.annotation.annotation.log.SysLog;
import com.eastfair.annotation.annotation.security.PreAuth;
import com.eastfair.boot.controller.SuperController;
import com.eastfair.boot.request.PageParams;
import com.eastfair.core.base.R;
import com.eastfair.core.context.ContextUtil;
import com.eastfair.core.utils.ConvertUtil;
import com.eastfair.echo.core.EchoService;
import com.eastfair.venueservice.dto.*;
import com.eastfair.venueservice.entity.ServicePriceListItem;
import com.eastfair.venueservice.service.ServicePriceListItemService;
import com.eastfair.venueservice.vo.ServicePriceListItemVO;
import com.eastfair.venueservice.vo.ServicePriceListVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.validation.constraints.NotEmpty;
import java.util.Collections;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;


/**
 * <p>
 * 前端控制器
 * 服务价目信息表
 * </p>
 *
 * @author linan
 * @date 2022-07-21
 */
@Slf4j
@Validated
@RestController
@RequestMapping("/servicePriceListItem")
@Api(value = "ServicePriceListItem", tags = "服务价目信息表")
@PreAuth(replace = "venueservice:servicePriceListItem:")
public class ServicePriceListItemController extends SuperController<ServicePriceListItemService, Long, ServicePriceListItem, ServicePriceListItemPageQuery, ServicePriceListItemSaveDTO, ServicePriceListItemUpdateDTO> {

    @Autowired
    private EchoService echoService;

    @Override
    public void handlerResult(IPage<ServicePriceListItem> page) {
        // 想让返回值实现自动回显，请将此行代码打开
        // echoService.action(page);
    }
    /**
     * Excel导入后的操作
     *
     * @param list
     */
    @Override
    public R<Boolean> handlerImport(List<Map<String, String>> list){
        List<ServicePriceListItem> servicePriceListItemList = list.stream().map((map) -> {
            ServicePriceListItem servicePriceListItem = ServicePriceListItem.builder().build();
            //TODO 请在这里完成转换
            return servicePriceListItem;
        }).collect(Collectors.toList());

        return R.success(baseService.saveBatch(servicePriceListItemList));
    }



    /**
     * 处理参数
     *
     * @param params 分页参数
     */
    @Override
    public void handlerQueryParams(PageParams<ServicePriceListItemPageQuery> params) {
        ServicePriceListItemPageQuery pageQuery = params.getModel();
        //分页处理
        if(ContextUtil.getProjectId()!=null){
            pageQuery.setProjectId(ContextUtil.getProjectId());
        }
    }

    /**
     * 添加价目服务项
     *
     * @param servicePriceListItems 添加价目服务项
     * @return R
     */
    @ApiOperation(value = "添加价目服务项", notes = "添加价目服务项")
    @SysLog("添加价目服务项")
    @PostMapping("/addPriceListItems")
    public R<Boolean> addPriceListItems(@RequestBody @Validated(ServicePriceListDTO.AddItem.class) ServicePriceListDTO servicePriceListItems) {
        log.info("addPriceListItems - 添加价目服务项 servicePriceListItems={}", servicePriceListItems);
        return R.success(baseService.addPriceListItems(servicePriceListItems));
    }

    /**
     * 更新价目下服务
     *
     * @param servicePriceListItems 更新价目下服务
     * @return R
     */
    @ApiOperation(value = "更新价目下服务", notes = "更新价目下服务")
    @SysLog("修改价目服务项")
    @PostMapping("/updatePriceListItem")
    public R<Boolean> updatePriceListItem(@RequestBody @Validated(ServicePriceListItemDTO.Update.class) @NotEmpty(message = "参数不能为空") List<ServicePriceListItemDTO> servicePriceListItems) {
        log.info("updatePriceListItem - 更新价目 servicePriceListItems={}", servicePriceListItems);
        try {
            return R.success(baseService.updatePriceListItem(servicePriceListItems));
        } catch (Exception e) {
            return R.fail(e);
        }
    }

    /**
     * 全量更新价目的开放对象
     *
     * @param servicePriceListDTO 全量更新价目的开放对象
     * @return R
     */
    @ApiOperation(value = "全量更新价目的开放对象", notes = "全量更新价目的开放对象")
    @SysLog("修改价目服务项")
    @PostMapping("/updateAllPriceListItem")
    public R<Boolean> updateAllPriceListItem(@RequestBody @Validated(ServicePriceListDTO.Update.class) ServicePriceListDTO servicePriceListDTO) {
        log.info("updateAllPriceListItem - 更新价目 servicePriceListDTO={}", servicePriceListDTO);
        try {
            return R.success(baseService.updateAllPriceListItem(servicePriceListDTO));
        } catch (Exception e) {
            return R.fail(e);
        }
    }

    /**
     * 查询价目下服务
     *
     * @param servicePriceListItem 查询价目下服务
     * @return R
     */
    @ApiOperation(value = "查询价目下服务", notes = "查询价目下服务")
    @PostMapping("/queryPriceListItem")
    public R<IPage<ServicePriceListItemVO>> queryPriceListItem(@RequestBody @Validated(ServicePriceListItemQuery.Query.class) PageParams<ServicePriceListItemQuery> servicePriceListItem) {
        log.info("queryPriceListItem - 查询价目 servicePriceListItem={}", servicePriceListItem);
        return baseService.queryPriceListItem(servicePriceListItem);
    }

    /**
     * 通过展会id,系统编码查询价目
     *
     * @param query 通过展会id,系统编码查询价目
     * @return R
     */
    @ApiOperation(value = "通过展会id,系统编码查询价目", notes = "通过展会id,系统编码查询价目")
    @PostMapping("/queryPriceListByExhibition")
    public R<ServicePriceListVO> queryPriceListByExhibition(@RequestBody @Validated(ServicePriceListQuery.Query.class) ServicePriceListQuery query) {
        log.info("queryPriceListByExhibition - 查询价目 servicePriceListItem={}", query);
        return baseService.queryPriceListByExhibition(query);
    }

    /**
     * 通过价目id查询价目详情
     *
     * @param query 通过价目id查询价目详情
     * @return R
     */
    @ApiOperation(value = "通过价目id查询价目详情", notes = "通过价目id查询价目详情")
    @PostMapping("/queryPriceListById")
    public R<ServicePriceListVO> queryPriceListById(@RequestBody @Validated(ServicePriceListQuery.Update.class) ServicePriceListQuery query) {
        log.info("queryPriceListById - 通过价目id查询价目详情 query={}", query);
        return baseService.queryPriceListById(query);
    }

    /**
     * 统一保存价目服务
     *
     * @param dto 统一保存价目服务
     * @return R
     */
    @ApiOperation(value = "统一保存价目服务", notes = "统一保存价目服务")
    @SysLog("添加价目服务项")
    @PostMapping("/saveAllPriceList")
    public R<Boolean> saveAllPriceList(@RequestBody ServicePriceListDTO dto) {
        log.info("saveAllPriceList - 统一保存价目服务 dto={}", dto);
        return baseService.saveAllPriceList(dto);
    }

    /**
     * 批量删除价目下服务
     *
     * @param servicePriceListItems 批量删除价目下服务
     * @return R
     */
    @ApiOperation(value = "批量删除价目下服务", notes = "批量删除价目下服务")
    @SysLog("删除价目服务项")
    @PostMapping("/delPriceListItemBatch")
    public R<Boolean> delPriceListItemBatch(@RequestBody @Validated(ServicePriceListItemDTO.Update.class) @NotEmpty(message = "参数不能为空") List<ServicePriceListItemDTO> servicePriceListItems) {
        log.info("servicePriceListItems - 批量删除价目下服务 delPriceListItemBatch={}", servicePriceListItems);
        return R.success(baseService.delPriceListItemBatch(servicePriceListItems));
    }

    /**
     * 删除价目
     *
     * @param servicePriceListItem 删除价目
     * @return R
     */
    @ApiOperation(value = "删除价目", notes = "删除价目")
    @SysLog("删除价目")
    @PostMapping("/delPriceListItem")
    public R<Boolean> delPriceListItem(@RequestBody @Validated(ServicePriceListItemDTO.Update.class) ServicePriceListItemDTO servicePriceListItem) {
        log.info("delPriceListItem - 删除价目 servicePriceListItem={}", servicePriceListItem);
        return R.success(baseService.removeByIdsOfLogic(ConvertUtil.convertList(Collections.singletonList(servicePriceListItem), ServicePriceListItem.class)));
    }

}
